#include "import.h"
#include <string>
#include <iostream>
#include <fstream>
#include "essential.h"
#include "..\math\vector.h"
#include "..\math\meshobject.h"
#include "..\engine\dynamicarray.h"
#include "assert.h"

using namespace std;

// ================================================================================================
// Import mesh information from a file
// ================================================================================================
void ImportTriangleMesh::Import(char* filepath) {
    // -- open file
    fstream mesh;
    if(filepath == NULL) 
        filepath = "mesh.txt";

    mesh.open(filepath, ios::in);

    // -- check for file failure
    if(mesh.fail()) {
        cout << "File " << filepath << " failed to load.\n";
        cout << "Exiting application.";
        exit(-1);
    }

    // -- get shapename
    string shapename;
    mesh >> shapename;

    // -- get vertices
    string vectortitle;
    uint numverts;
    mesh >> vectortitle >> numverts;
    Vector4 verts;
    for(uint i = 0; i < numverts; ++i) {
        mesh >> verts.x;
        mesh >> verts.y;
        mesh >> verts.z;
        vertices.Append(verts);
    }

   // -- get verts per face
    string vertsper;
    uint numvertsper;
    mesh >> vertsper >> numvertsper;
    ushort vertsperface;
    for(uint i = 0; i < numvertsper; ++i) {
        mesh >> vertsperface;
        vertsperfaces.Append(vertsperface);
    }

    // -- get indices
    string index;
    uint numindices;
    mesh >> index >> numindices;
    ushort indice;
    for(uint i = 0; i < numindices; ++i) {
        mesh >> indice;
        indices.Append(indice);
    }

}

// ================================================================================================
// Create the mesh 
// ================================================================================================
MeshObject ImportTriangleMesh::GetMesh() {
    uint current = 0;
    uint count;
    MeshObject mobject;
    for(uint i = 0; i < vertsperfaces.GetSize(); ++i) {
        mobject.GetNumFaces()++;
        if(vertsperfaces[i] == 3)
            CreateTriangle(current, mobject);

        if(vertsperfaces[i] == 4) 
            CreateTriangleSquare(current, mobject);

        count = vertsperfaces[i];
        current += count;
    }

    return mobject;
}

// ================================================================================================
// Create a single triangle
// ================================================================================================
void ImportTriangleMesh::CreateTriangle(uint current, MeshObject& mesh) {
    Vector4 vecs[3];
    uint j = 0;
    for(uint i = current; i < current + 3; ++i) {
        vecs[j].x = vertices[indices[i]].x;
        vecs[j].y = vertices[indices[i]].y; 
        vecs[j].z = vertices[indices[i]].z; 
        vecs[j].w = 1.0f;
        ++j;
    }

    //Color col = {1.0f, 0.0f, 1.0f};
    Triangle tri = {vecs[0], vecs[1], vecs[2]};

    TriangleSurface trisurface(/*col,*/ tri);

    mesh.AppendFace(trisurface);
}

// ================================================================================================
// Create a square based off of two triangles
// ================================================================================================
void ImportTriangleMesh::CreateTriangleSquare(uint current, MeshObject& mesh) {
    // -- since we are splitting the face into two we must add another face
    mesh.GetNumFaces()++;

    Vector4 sqr[4];
    int j = 0;
    for(uint i = current; i < current + 4; ++i) {
        sqr[j].x = vertices[indices[i]].x;
        sqr[j].y = vertices[indices[i]].y; 
        sqr[j].z = vertices[indices[i]].z; 
        sqr[j].w = 1.0f;
        ++j;
    }

    //Color col1 = {0.0f, 0.0f, 1.0f};
    //Color col2 = {1.0f, 0.0f, 0.0f};
    Triangle tri1 = {sqr[0], sqr[1], sqr[2]};
    Triangle tri2 = {sqr[0], sqr[2], sqr[3]};

    TriangleSurface trisurface1(/*col1,*/ tri1);
    TriangleSurface trisurface2(/*col2,*/ tri2);
    mesh.AppendFace(trisurface1);
    mesh.AppendFace(trisurface2);
}


